package com.triones.ljr.controller.web;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.triones.ljr.entity.dto.IndustryDO;
import com.triones.ljr.entity.vo.IndustryVO;
import com.triones.ljr.service.IndustryService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行业分类
 *
 * @Author: triones
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/industry")
public class IndustryController extends BaseController {

    private final IndustryService industryService;

    /**
     * 列表（分页）
     */
    @SaCheckPermission("system:industry:pageList")
    @GetMapping("/pageList")
    public TableDataInfo<IndustryVO> pageList(String keyword, PageQuery pageQuery) {
        return industryService.pageList(keyword, pageQuery);
    }

    /**
     * 新增
     */
    @SaCheckPermission("system:industry:add")
    @Log(title = "行业分类管理", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated @RequestBody IndustryDO dto) {
        if (!industryService.checkNameUnique(dto)) {
            return R.fail("新增行业分类'" + dto.getName() + "'失败，行业分类已存在");
        }
        return toAjax(industryService.add(dto));
    }

    /**
     * 修改
     */
    @SaCheckPermission("system:industry:edit")
    @Log(title = "行业分类管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated @RequestBody IndustryDO dto) {
        if (!industryService.checkNameUnique(dto)) {
            return R.fail("修改行业分类'" + dto.getName() + "'失败，行业分类已存在");
        }
        return toAjax(industryService.edit(dto));
    }

    /**
     * 获取详情
     */
    @GetMapping("/{id}")
    public R<IndustryVO> getInfo(@PathVariable Long id) {
        return R.ok(industryService.getInfo(id));
    }

    /**
     * 删除
     */
    @SaCheckPermission("system:industry:remove")
    @Log(title = "行业分类管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public R<Void> remove(@PathVariable Long id) {
        return toAjax(industryService.remove(id));
    }

    /**
     * 列表（所有）
     */
    @GetMapping("/list")
    public R<List<IndustryVO>> list() {
        return R.ok(industryService.industryList());
    }
}
